$(function() {	
	$('#city').change(function() {
		if($('#city').val() == 'other')
			$('#other_city').show();
		else
			$('#other_city').hide();
	});

	$('input#valider').click(function() {
		$('div.ajax').html('<img src="images/ajax-loader.gif" alt="Enregistrement en cours..." />');

		var id = $('#id').val(),
			mode = $('#mode').val(),
			name = $('#name').val(),
			firstname = $('#firstname').val(),
			email = $('#email').val(),
			pass = $('#pass').val(),
			pass2 = $('#pass2').val(),
			homephone = $('#homephone').val(),
			cellphone = $('#cellphone').val(),
			address = $('#address').val(),
			postal_code = $('#postal_code').val(),
			city = $('#city').val() != 'other' ? $('#city').val() : $('#other_city').val(),
			birth = getBirth(),
			gymnasium = $('#gymnasium').val(),
			category = $('#category').val(),
			namep = $('#namep').val(),
			firstnamep = $('#firstnamep').val(),
			emergency_phone = $('#emergency_phone').val(),
			emailp = $('#emailp').val(),
			gender = $('input#gender_M').is(':checked') ? 'M' : $('input#gender_F').is(':checked') ? 'F' : 'err',
			climbing_years_practice = $('select#climbing_years_practice').val(),
			climbing_level = $('select#climbing_level').val(),
			competitions = $('input#competitions_1').is(':checked') ? 1 : $('input#competitions_0').is(':checked') ? 0 : 'err',
			has_pass92 = $('input#has_pass92_1').is(':checked') ? 1 : $('input#has_pass92_0').is(':checked') ? 0 : 'err',
			need_registration_proof = $('input#need_registration_proof_1').is(':checked') ? 1 : $('input#need_registration_proof_0').is(':checked') ? 0 : 'err',
			authorize_image_use = $('input#authorize_image_use_1').is(':checked') ? 1 : $('input#authorize_image_use_0').is(':checked') ? 0 : 'err';
			
		$.ajax({
			url: 'pages/inscription/enregistrement.php',
			type: 'POST',
			data: 'id='+id
				+ '&mode='+mode
				+ '&name='+name
				+ '&firstname='+firstname
				+ '&email='+email
				+ '&pass='+pass
				+ '&pass2='+pass2
				+ '&homephone='+homephone
				+ '&cellphone='+cellphone
				+ '&address='+address
				+ '&postal_code='+postal_code
				+ '&city='+city
				+ '&birth='+birth
				+ '&gymnasium='+gymnasium
				+ '&category='+category
				+ '&namep='+namep
				+ '&firstnamep='+firstnamep
				+ '&emergency_phone='+emergency_phone
				+ '&emailp='+emailp
				+ '&gender='+gender
				+ '&climbing_years_practice='+climbing_years_practice
				+ '&climbing_level='+climbing_level
				+ '&competitions='+competitions
				+ '&has_pass92='+has_pass92
				+ '&need_registration_proof='+need_registration_proof
				+ '&authorize_image_use='+authorize_image_use
				+ ($('#check_transport:checked').val() != null ? '&check_transport=1' : '')
				+ ($('#check_reglement:checked').val() != null ? '&check_reglement=1' : ''),
				
			success: function(result) {
				// Edition ou erreur, on laisse le formulaire
				$('div.ajax').html('');
				$('div#ajax_result').html(result);
				// On remonte en haut de la page
				$(document).scrollTop(0);				
			},
			error: function(xhr, textStatus, errorThrown) {
				// Inscription ou ajout membre géré
				$('div.ajax').html('');
				$('div#ajax_result').load('pages/inscription/confirmation.php', function() {					
					if(xhr.status == 400)
						$('div#ajax_result').prepend('<p class="success">Inscription réussie, un e-mail de confirmation avec vos identifiants vient de vous être envoyé</p><p class="warning"><strong>Attention</strong>, il arrive que nos e-mails soient placés dans les courriers indésirables (spam).</p>')
					else if(xhr.status == 417)
						$('div#ajax_result').prepend('<p class="warning">L\'inscription a réussie, mais l\'envoi de l\'e-mail de confirmation n\'a pas fonctionné.</p>')				
					// On remonte en haut de la page
					$(document).scrollTop(0);
					$('div#form_inscription').hide();
				});
			}
		});
		
		return false;
	});
});
